
const multer= require("multer");
const fs = require("fs");

const dbPool=require("../config/dbPool")
const jwt=require("jsonwebtoken")
var QcloudSms = require("qcloudsms_js");
const userController={
        getAllMovie(req,resp){
         // console.log(req.query.input)
         dbPool.connect("select * from movies",[],(err,data)=>{
             if(!err){
                if(data.length>0){
                     resp.send(data)
                     
                }else{
                  resp.send(data)
              }

             }
             
         })    
        },
        enroll(req,resp)
     {
     
        user=req.query.user,
        oldPassword=req.query.oldPassword
        newPassword=req.query.newPassword
      //   console.log(newPassword)
        if(oldPassword===newPassword){
         dbPool.connect("insert into usertable (user,password,date) values(?,?,?)",[user,newPassword,req.query.date],(err,data)=>{
                if(!err){
                  resp.send("注册成功")
                     
                }else{
                   resp.send(err)

                }
             
         })

        }else if(oldPassword!=newPassword){
              resp.send("密码不一致，请重新输入")
        }

     },
     login(req,resp){
        
      if(req.query.user=="root"&&req.query.password=="root"){
               resp.send("ok")
      }else{
         dbPool.connect("select * from usertable where user=? and password=?",[req.query.user,req.query.password],(err,data)=>{
            if(!err){  
               if(data.length>0){
                  data[0].state="true"
                  let newData=data
         dbPool.connect("UPDATE usertable SET state='true' WHERE id=?",[newData[0].id],(err,data)=>{
                           //  console.log(newData[0].id)
         })


                 const token=jwt.sign({
                  user:req.query.user,password:req.query.password
                 },"tom",{expiresIn:"1h"})
                 resp.send([token,data])
              
        
                   
               }
       

            }
       
     })

      }

       
         
  },
  updateState(req,resp){
      //   console.log(req.query.id)
      dbPool.connect("UPDATE usertable SET state='false' WHERE id=?",[req.query.id],(err,data)=>{
        
      })
  }
  ,
  anime(req,resp){
      dbPool.connect("select * from movies where type=? ",["奇幻"],(err,data)=>{
             if(!err){
               if(data.length>0){
                    resp.send(data)
               }
             }
      })
     
  },
  //评论
  addName(req,resp){
//   console.log(req.query.names)
    dbPool.connect("select * from `comment` where `name`=?",[req.query.names],(err,data)=>{
             if(!err){
                if(!data.length){
                  // console.log("wu")
                       dbPool.connect("insert into comment (name) values(?)",[req.query.names],(err,data)=>{
                                        
                       })
                }else{
                  console.log(err)
                }
                 
             }
    })
  },
  addComment(req,resp){
      //  dbPool.connect("select * from `comment`",[],(err,data)=>{
      //              if(!err){
      //                  if(data.length==1){
      //                     dbPool.connect("UPDATE `comment` SET `imgSrc`=? ,`content`=? ,`date`=?, title=? WHERE name=?",[req.query.imgSrc,req.query.content,req.query.date,req.query.MyTitle,req.query.name],(err,data)=>{
      //                         if(!err){
      //                                console.log("ok")
      //                         }else{
      //                              console.log(err)
      //                         }
      //                     })
                           
      //                  }
                       
      //              }
      //  })

     
   dbPool.connect("insert into comment (`name`,imgSrc,content,`date`,title) values(?,?,?,?,?)",[req.query.name,req.query.imgSrc,req.query.content,req.query.date,req.query.MyTitle],(err,data)=>{
                                 if(!err){
                                      console.log("添加成功")
                                 }
   })
     
  },
  getComment(req,resp){
   //  console.log(req.query.MyTitle)
   dbPool.connect("select * from comment where title=?",[req.query.MyTitle],(err,data)=>{
            if(!err){
                 if(data.length>0){
                    
                     resp.send(data)
                 }
            }else{
                console.log(err)
            }
   })
  }
  ,
  delComment(req,resp){
   //   console.log(req.query.content)
   dbPool.connect("DELETE FROM comment WHERE content =?",[req.query.content],(err,data)=>{
                   if(!err){
                      console.log("删除成功")
                      resp.send("删除成功")
                   }
   })
  }
  ,
  banner(req,resp){
   dbPool.connect("select * from banner",[],(err,data)=>{
      if(!err){
         if(data.length>0){
              resp.send(data)
              
         } 

      }
      
  })
       
  },
// userNode(req,resp){
//        dbPool.connect("select * from usertable",[],(err,data)=>{
//                    if(!err){
//                        if(data.length>0){
                          
//                          resp.send(data)
//                        }
//                    }
//        })



//   },
  deletes(req,resp){
   dbPool.connect("DELETE FROM usertable WHERE id =?",[req.query.id],(err,data)=>{
              if(!err){
                 resp.send("删除成功")
              }
   })
  }  

,
   // 修改用户数据
   updateUser(req,resp){
      
  dbPool.connect("UPDATE usertable SET `user`=? ,`password`=? ,`name`=? ,`like`=?, phone=? ,state=? WHERE id=?",[req.query.user,req.query.password,req.query.name,req.query.like,req.query.phone,req.query.state,req.query.id],(err,data)=>{
               if(!err){
                   resp.send("修改成功")
               }else{
                  console.log(err)
               }
  })


   },
   
 upImg(req,resp){

//   console.log(req.file)
 let pathname=req.file.filename
  let userPathName= pathname.split(".")[0]
//   console.log(userPathName)
//   console.log(pathname)
//   console.log(req.body.id)
  dbPool.connect("UPDATE usertable SET imgSrc=? WHERE id=?",[userPathName,req.body.id],(err,data)=>{
             if(!err){
               resp.send("头像更换成功")
                 
             }                          
  })
     
   },
   updatePerson(req,resp){
      // console.log(req.query.person[0].like)
      dbPool.connect("UPDATE usertable SET `user`=?,`password`=?,`name`=?,phone=?,`like`=? WHERE id=?",[req.query.person[0].user,req.query.person[0].password,req.query.person[0].name,req.query.person[0].phone,req.query.person[0].like,req.query.person[0].id],(err,data)=>{
         if(!err){
           dbPool.connect("select * from usertable where id =?",[req.query.person[0].id],(err,data)=>{
               if(!err){
                  // console.log("hh")
                    resp.send(data)
               }
           })
         }
})

    
       
   }
   ,
   searchUser(req,resp){
      if(req.query.user){
         dbPool.connect("select * from usertable where user=?",[req.query.user],(err,data)=>{
             if(!err){
                if(data.length>0){
                    resp.send(data)
                }else{
                  resp.send(data)
                }
             }
         })
      } else if(req.query.user==""){
            dbPool.connect("select * from usertable",[],(err,data)=>{
                  if(!err){
                      resp.send(data)
                  }
            })
          
      }
       
       
   },
   getUserDate(req,resp){
      dbPool.connect("select * from usertable",[],(err,data)=>{
         if(!err){
             if(data.length>0){
                start=(req.query.currentPage-1)*req.query.pagesize
                last= req.query.currentPage*req.query.pagesize
                newArr=data.slice(start,last)
                allTotal=data.length
                endArr=[newArr,allTotal]
                resp.send(endArr)
             
             }
         }
})
         
   },
   addUser(req,resp){
      // console.log("--------------")
      //     console.log(req.query.name) 
      //     console.log(req.query.user) 
      //     console.log(req.query.password) 
      console.log(req.query.date)
    dbPool.connect("INSERT INTO usertable(`name`,`user`,`password`,phone,`like`,state,`date`) VALUES(?,?,?,?,?,'false',?)",[req.query.name,req.query.user,req.query.password,req.query.phone,req.query.like,req.query.date],(err,data)=>{
               if(!err){
                    resp.send("添加成功")
               }else{
                  console.log(err)
               }
    })
       
   },

   //短信验证码登录
   phone(req,resp){ 
     
var appid = 1400187558;  

var appkey = "dc9dc3391896235ddc2325685047edc7";

var phoneNumbers = [req.body.phone];

var templateId = 285590;  

var smsSign = "白泽影视提示";  
var qcloudsms = QcloudSms(appid, appkey);

function callback(err, ress, resData) {
    if (err) {
        console.log("err: ", err);
    } else {
     resp.send({
       data:ress.req.body.params[0]
     })
    }
}
var ssender = qcloudsms.SmsSingleSender();
var params = [Math.floor(Math.random()*(9999-1000))+1000];
   ssender.sendWithParam(86, phoneNumbers[0], templateId,params, smsSign, "", "",callback);  
   },
   phoneLogin(req,resp){
            dbPool.connect("select * from usertable where phone=?",[req.query.phone],(err,data)=>{
                               if(!err){
                                    if(data.length>0){
                                       let newData=data
                              dbPool.connect("UPDATE usertable SET state='true' WHERE id=?",[newData[0].id],(err,data)=>{
                                             
                              })    
                    const token=jwt.sign({
                  user:req.query.user,password:req.query.password
                 },"tom",{expiresIn:"1h"})
                      resp.send([token,data])
                                          
                                    }else{
                                       resp.send("手机号不存在，请先注册")
                                    }
                               }else{
                                  console.log(err)
                               }
            })
            
   }
  
      }
  
   
       







module.exports=userController